package cn.tedu.supermarket.controller;

import cn.tedu.supermarket.entity.BannersDTO;
import cn.tedu.supermarket.entity.User;
import cn.tedu.supermarket.mapper.BannersMapper;
import cn.tedu.supermarket.service.GoodsImagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.List;

@Controller
public class BannersController {
    @Autowired
    private GoodsImagesService goodsImages;
    @Autowired
    private BannersMapper bannersMapper;

    //遍历显示轮播图
    @RequestMapping("/goUpdateBanners")
    public String goBanners(HttpSession session, Model model){
        if (session.getAttribute("user")!=null){
            List<BannersDTO> banners = bannersMapper.select();
            model.addAttribute("banners",banners);
            return "sel_user/banners";
        }
        return "redirect:sLoginNow";
    }
    //添加轮播图
    @RequestMapping("/addBanners")
    public String addBanners(@RequestParam("file") MultipartFile file,HttpSession session) {
        if (session.getAttribute("user")!=null){
            int row = goodsImages.addGoodsImages(file);
            Integer a = bannersMapper.insert(goodsImages.getPaths());
            return "redirect:goUpdateBanners";
        }
        return "redirect:sLoginNow";
    }
    //删除轮播图
    @RequestMapping("/deletebanner")
    public String deleteGoods(int id, HttpSession session){
        if (session.getAttribute("user")!=null) {
            System.err.println(id);
            String url = bannersMapper.selectById(id);

            String path = "/home/soft01"+url;//需要修改处
            
            System.err.println("path="+path);
            File file = new File(path);
            file.delete();
            bannersMapper.delete(id);
            return "redirect:goUpdateBanners";
        }
        return "redirect:sLoginNow";
    }
}
